{% extends "site_base.html" %}
{% load i18n %}
{% load qa_tags %}
{% load qatrack_tags %}
{% load attach_tags %}
{% load comments %}
{% load static %}

{% block head_title %}{{test_list_instance.work_completed|date}} {{test_list_instance.test_list.name}} Details{% endblock %}

{% block extra_css %}
  <link href="{% static "qatrack_core/css/tables.css" %}?v={{VERSION}}" rel="stylesheet">
  <link href="{% static "qa/css/qa.css" %}?v={{VERSION}}" rel="stylesheet">
{% endblock extra_css %}

{% block extra_js %}
    <script type="text/javascript">
        var override_date = false;
    </script>
{% endblock %}

{% block require_javascript %}
    require(['jquery', 'qareview'], function() {
    });
{% endblock require_javascript %}

{% block body %}
    <div class="row">
        <div class="col-md-12">

            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">{{test_list_instance.work_completed|date}} {{test_list_instance.test_list.name}} Details</h3>
                    {% if cycle_name %}
                        <div><em>From cycle : {{ cycle_name }}</em></div>
                    {% endif %}
                </div>

                <div class="box-body">

                    <div class="row">

                        {% with tli=test_list_instance %}
                            <div class="col-md-4">
                                <dl class="dl-horizontal">
                                    <dt>Unit :</dt>
                                    <dd>{{tli.unit_test_collection.unit.name}}</dd>

                                    <dt>Collection :</dt>
                                    <dd>{{tli.unit_test_collection.tests_object.name}}</dd>

                                    <dt>Test List :</dt>
                                    <dd>
                                        {% if tli.test_list.description%}
                                        <a class="show-hide" id="test-list-info-toggle" title="Click to view description and general instructions for this test list">{{tli.test_list.name}} <i class="fa fa-info" ></i></a>
                                        {% else %}
                                           {{tli.test_list.name}}
                                        {% endif %}
                                    </dd>

                                    <dt>Performed By :</dt>
                                    <dd>{{tli.created_by}}</dd>

                                    <dt>User Key:</dt>
                                    <dd>
                                      {% if tli.user_key %}
                                        {{tli.user_key }}
                                      {% else %}
                                        <em>{% trans "Not set" %}</em>
                                      {% endif %}
                                    </dd>
                               </dl>
                            </div>
                            <div class="col-md-4">
                                <dl class="dl-horizontal">

                                    <dt>Work Started :</dt>
                                    <dd>{{tli.work_started}}</dd>

                                    <dt>Work Completed :</dt>
                                    <dd>
                                        {% if tli.in_progress %}
                                            <span class="label label-warning">
                                                <em>In Progress</em>
                                            </span>
                                        {% else %}
                                            <span class="label label-success">
                                                {{tli.work_completed}}
                                            </span>
                                        {% endif %}
                                    </dd>

                                    <dt>Duration :</dt>
                                    <dd>
                                        {% if not tli.in_progress%}
                                            {{tli.duration|as_time_delta}}
                                        {% else %}
                                            <em>In Progress</em>
                                        {% endif %}
                                    </dd>

                                    <dt>Previous Due Date:</dt>
                                    <dd>
                                      {% if tli.due_date %}
                                        <abbr title="Due date when this test list was performed">{{ tli.due_date }}</abbr>
                                      {% else %}
                                        <em>No due date set</em>
                                      {% endif %}
                                    </dd>

                                    <dt>Scheduled:</dt>
                                    <dd>
                                      {% if tli.include_for_scheduling %}
                                        <abbr title="This instance is included when calculating due dates">Yes</abbr>
                                      {% else %}
                                        <abbr title="This instance is not considered when calculating due dates">No</abbr>
                                      {% endif %}
                                    </dd>

                                    <dt>Last Modification :</dt>
                                    <dd>{{tli.modified_by}} @ {{tli.modified}}</dd>

                                    <dt>Reviewed by:</dt>
                                    <dd>
                                      {% if tli.reviewed %}
                                        {{tli.reviewed_by}} @ {{tli.reviewed}}
                                      {% else %}
                                        <em>{% trans "Unreviewed" %}</em>
                                      {% endif %}
                                    </dd>
                               </dl>
                            </div>
                            <div class="col-md-4">
                                {% if perms.reports.can_run_reports or perms.reports.can_create_reports %}
                                  <div class="row">
                                    <div class="col-sm-12 text-right">
                                      {% include "qa/testlistinstance_pdf_button.html" with tli=tli %}
                                    </div>
                                  </div>
                                {% endif %}
                                <div class="row">
                                    <label class="col-md-8">
                                        {% if perms.service_log.add_serviceevent %}
                                            <div class="btn-group">
                                                <a class="btn btn-default btn-xs btn-flat hover-parent"
                                                    title="Create new service event initiated by this list"
                                                    href="{% url 'sl_new' %}?ib={{ tli.pk }}&next={{request.path|cut:"data/"}}"
                                                >
                                                    <i class="fa fa-stack fa-fw">
                                                        <i class="fa fa-wrench fa-stack-custom-main"></i>
                                                        <i class="fa fa-plus fa-stack-custom-sub upper-0 hover-sub-success"></i>
                                                    </i>
                                                    {% if service_events_ib|length == 0 %}Initiate Service Event{% endif %}
                                                </a>
                                                <a class="btn btn-default btn-xs btn-flat hover-parent new-tab-link"
                                                    title="New tab: Create new service event initiated by this list"
                                                    href="{% url 'sl_new' %}?ib={{ tli.pk }}&next={{request.path|cut:"data/"}}"
                                                    target="_blank"
                                                >
                                                    <i class="fa fa-chevron-right" aria-hidden="true"></i>
                                                </a>
                                            </div>
                                        {% endif %}
                                        {% if service_events_ib|length > 0 %}Service Events Initiated:{% endif %}
                                    </label>
                                    <div class="col-md-4">
                                        {% for se in service_events_ib %}
                                            {% if perms.service_log.view_serviceevent %}
                                                <div class="btn-group">
                                                    <a href="{% url 'sl_details' pk=se.id %}"
                                                        class="btn btn-xs btn-flat service-event-btn"
                                                        data-bgcolour="{{ se.service_status.colour }}"
                                                        title="View service event"
                                                    >{{ se.id }}</a>
                                                    <a class="btn btn-default btn-xs btn-flat new-tab-link service-event-btn"
                                                        href="{% url 'sl_details' pk=se.id %}"
                                                        target="_blank"
                                                        data-bgcolour="{{ se.service_status.colour }}"
                                                        title="New tab: view service event"
                                                    >
                                                        <i class="fa fa-chevron-right" aria-hidden="true"></i>
                                                    </a>
                                                </div>
                                            {% else %}
                                                <div>{{ se.id }}</div>
                                            {% endif %}
                                        {% endfor %}
                                    </div>
                                </div>

                                {% if service_events_rtsqa|length > 0 %}
                                    <div class="row margin-top-10">
                                        <label class="col-md-8">
                                            RTS QC for Service Events:
                                        </label>
                                        <div class="col-md-4">
                                            {% for se in service_events_rtsqa %}
                                                {% if perms.service_log.view_serviceevent %}
                                                    <div class="btn-group">
                                                        <a href="{% url 'sl_details' pk=se.id %}"
                                                            class="btn btn-xs btn-flat service-event-btn"
                                                            data-bgcolour="{{ se.service_status.colour }}"
                                                            title="View service event"
                                                        >{{ se.id }}</a>
                                                        <a class="btn btn-default btn-xs btn-flat new-tab-link service-event-btn"
                                                            href="{% url 'sl_details' pk=se.id %}"
                                                            target="_blank"
                                                            data-bgcolour="{{ se.service_status.colour }}"
                                                            title="New tab: view service event"
                                                        >
                                                            <i class="fa fa-chevron-right" aria-hidden="true"></i>
                                                        </a>
                                                    </div>
                                                {% else %}
                                                    <div>{{ se.id }}</div>
                                                {% endif %}
                                            {% endfor %}
                                        </div>
                                    </div>
                                {% endif %}
                            </div>

                        </div>
                        {% if tli.test_list.description %}
                            <div class="row">
                                <div class="col-md-4">
                                    <dl class="dl-horizontal">
                                        <dt>Description:</dt>
                                    </dl>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    {% spaceless %}
                                        <div class="col-lg-12">
                                            <div class="text-display pre">{{tli.test_list.description.strip|safe|linebreaks}}</div>
                                        </div>
                                    {% endspaceless %}
                                </div>
                            </div>
                        {% endif %}

                    {% endwith %}

                </div>
            </div>
        </div>
    </div>

{#    <div class="review-test-list" id="qa-review" >#}
{##}
{#        #}
{#    </div>#}


        {% if test_list_instance.test_list.description %}
        <div id="test-list-info" class="row" style="display:none;">
            <div class="col-md-12">
                <pre >
                    {{test_list_instance.test_list.description|safe|linebreaks}}
                </pre>
            </div>
        </div>
        {% endif %}
    <div class="row">
        <div class="col-md-12">

            <div class="box">
                <div class="box-header">

                </div>

                <div class="box-body">
                    <div class="row">
                        <div class="col-md-12">
                            {% with test_list_instance.history as instances_and_dates  %}
                            <table id="review-table" class="table table-bordered table-striped table-condensed" >
                                <thead>
                                    <tr>
                                        <th>Test<div></div></th>
                                        <th class="review-value">Value</th>
                                        <th class="review-ref">Reference</th>
                                        <th>Comment</th>
                                        <th class="qa-history">History<br/>
                                            {% for tli_url, d in instances_and_dates.1 %}
                                              <a href="{{ tli_url}}" title="{% blocktrans %}Click to view this test list instance performed at {{ d }}{% endblocktrans %}">
                                                <span class="label history-label">{{d|date:"dMy"}}</span>
                                              </a>
                                            {% endfor %}
                                            <div></div>
                                        </th>
                                        <th class="review-status">Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for ti, history in instances_and_dates.0 %}

                                        {% with test=ti.unit_test_info.test %}
                                            {% with borders.starts|lookup:forloop.counter0 as delim %}
                                              {% if delim and delim.class == "sublist" %}
                                                <tr class="{{delim.class }}">
                                                  <td colspan="2">
                                                    {% if delim.description %}
                                                      <a class="show-sublist-details" href="#">
                                                        {{ delim.name }}
                                                      </a>
                                                    {% else %}
                                                        {{ delim.name }}
                                                    {% endif %}
                                                </td>
                                                <td colspan="{% qa_table_colspan perms offset=2 %}">&nbsp;</td>
                                              </tr>
                                              {% endif %}
                                            {% endwith %}
                                          <tr>
                                              <td title="{{ ti.unit_test_info.test.name }}"><strong>{{ test.display }}</strong></td>
                                              <td class="review-value">
                                                {% with vd=ti.value_display %}
                                                  <span class="label pass-fail {{ti.pass_fail}}" title="{% if ti.string_value %}{{vd|slice:"100"}}{% elif ti.diff_display %}{% trans "Diff from ref" %}={{ti.diff_display}}{%else%}{% trans "No ref/tol set" %}{% endif %}">
                                                    {% with ti.pass_fail as status %}
                                                      {% if ICON_SETTINGS.SHOW_STATUS_ICONS_REVIEW %}
                                                          {% if status == 'tolerance' %}
                                                              <i class="pull-left fa fa-exclamation-circle" aria-hidden="true"></i>
                                                          {% elif status == 'action' %}
                                                              <i class="pull-left fa fa-ban" aria-hidden="true"></i>
                                                          {% elif status == 'ok' %}
                                                              <i class="pull-left fa fa-check-circle-o" aria-hidden="true"></i>
                                                          {% elif status == 'not_done' %}
                                                              <i class="pull-left fa fa-circle-o" aria-hidden="true"></i>
                                                          {% endif %}
                                                      {% endif %}
                                                    {% endwith %}
                                                    {% if test.is_upload %}
                                                        <a href="#!" class="revealtext" title="{% trans "Click to show full results" %}"><i class="fa fa-book"></i></a>
                                                    {% elif vd|length > 100 %}
                                                      <a href="#!" class="revealtext" title="{{ vd|slice:"100" }}...{% trans "Click to show full text" %}"><i class="fa fa-book"></i></a>
                                                    {% else %}
                                                      {{ti.value_display|safe}}
                                                    {% endif %}
                                                  </span>
                                                {% endwith %}
                                              </td>
                                              <td class="review-ref">
                                                  {% reference_tolerance_span test ti.reference ti.tolerance%}
                                              </td>
                                              <td class="qa-showcmt">
                                                  {% if ti.comment %}
                                                      <a href="#!" class="revealcomment" title="{{ ti.comment }}"><i class="fa fa-commenting"></i></a>
                                                  {% endif %}
                                              </td>
                                              <td class="qa-history">
                                                  {% history_display history test_list_instance.unit_test_collection.unit test_list_instance.test_list test test_list_instance.unit_test_collection.frequency %}
                                              </td>
                                              <td class="review-status">
                                                  {{ti.status.name}}
                                              </td>
                                          </tr>

                                          {% if ti.comment %}
                                              <tr class="qa-comment no-hover row-{{ ti_form.prefix }}">
                                                  <td colspan="{% qa_table_colspan perms %}"  class="">
                                                      <div class="comment-container comment-bar">
                                                          <div class="comment-div">
                                                              {{ ti.comment }}
                                                          </div>
                                                      </div>
                                                  </td>
                                              </tr>
                                          {% endif %}

                                          {% if not test.is_upload and ti.string_value|length > 100 %}
                                            <tr class="qa-text-display no-hover row-{{ ti_form.prefix }} hidden">
                                              <td colspan="{% qa_table_colspan perms %}"  class="">
                                              <pre style="">
                                                {{ ti.value_display }}
                                              </pre>
                                              </td>
                                            </tr>
                                          {% elif test.is_upload %}
                                            <tr class="qa-text-display no-hover row-{{ ti_form.prefix }} hidden">
                                              <td colspan="{% qa_table_colspan perms %}"  class="">
                                              <pre style="">
                                                {% firstof ti.json_value|pprint "" %}
                                              </pre>
                                              </td>
                                            </tr>
                                          {% endif %}
                                          {% if ti.attachment_set.exists %}
                                              <tr class="qa-attach no-hover">
                                                  <td>{{ti.unit_test_info.test}} Attachments</td>
                                                  <td colspan="{% qa_table_colspan perms offset=1 %}"  class="">
                                                      <ul>
                                                          {% for attach in ti.attachment_set.all %}
                                                              <li>{{ attach | attachment_link }}</li>
                                                          {% endfor %}
                                                      </ul>
                                                  </td>
                                              </tr>
                                          {% endif %}
                                        {%endwith%}

                                        {% if not forloop.last and borders.ends|lookup:forloop.counter0 %}
                                          {% with borders.starts|lookup:forloop.counter as delim %}
                                            {% if not delim or delim.class != "sublist" %}
                                            <tr class="end-sublist">
                                                <td colspan="{% qa_table_colspan perms %}"></td>
                                            </tr>
                                            {% endif %}
                                          {% endwith %}
                                        {% endif %}

                                    {% endfor %}
                                    {% if test_list_instance.attachment_set.exists %}
                                        <tr>
                                            <td><strong>Attachments</strong></td>

                                            <td id="tli-attachment-names" colspan="{% qa_table_colspan perms offset=2 %}">
                                                {% for attach in test_list_instance.attachment_set.all %}
                                                    <i class="fa fa-paperclip fa-fw" aria-hidden="true"></i>{{ attach | attachment_link }}
                                                {% endfor %}
                                            </td>
                                        </tr>
                                    {% endif %}
                                </tbody>
                            </table>
                            {% endwith %}
                        </div>

                    </div>
                </div>
                <div class="box-footer">
                    {% if perms.qa.delete_testlistinstance %}
                      <a class="btn btn-danger btn-flat" title="This will redirect you to a page where you can permanently delete this qa session" href="{% url 'delete_test_list_instance' pk=test_list_instance.pk %}">Delete</a>
                    {% endif %}
                    {% if test_list_instance.in_progress %}
                        <a class="btn btn-default pull-right btn-flat margin-r-5" title="Continue this list" href="{%url 'continue_tli' pk=test_list_instance.pk%}?next={{request.get_full_path}}" >Continue</a>
                    {% elif perms.qa.change_testlistinstance %}
                        <a class="btn btn-default pull-right btn-flat margin-r-5" title="Edit this list" href="{%url 'edit_tli' pk=test_list_instance.pk%}?next={{request.get_full_path}}" >Edit</a>
                    {% endif %}
                    {% if perms.qa.can_review %}
                        <a href="{% url 'review_test_list_instance' pk=test_list_instance.pk %}?next={{request.get_full_path}}" class="btn pull-right btn-default btn-flat margin-r-5">Review</a>
                    {% endif %}
                </div>

                <div id="qa-images" class="box-footer">
                    {% for ti, history in test_list_instance.history.0 %}
                        {% with test=ti.unit_test_info.test %}
                            {% if test.display_image %}
                                <div class="row">
                                    <div class="col-sm-12">
                                        <strong><p>Images for {{ test }} Test: </p></strong>
                                    </div>
                                </div>
                                <div class="row">
                                    <div id="{{ test.slug }}">
                                        {% for attach in ti.attachment_set.all %}
                                            {% if attach.is_image %}
                                                {{ attach | ti_attachment_img }}
                                            {% endif %}
                                        {% endfor %}
                                    </div>
                                </div>
                            {% endif %}
                        {% endwith %}
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>

    <div id="box-comments" class="row">
        <div class="col-sm-12">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">
                        <i class="fa fa-commenting-o fa-fw"></i>
                        Comments
                    </h3>
                </div>
                <div class="box-body">
                    <div class="row">
                        <div class="col-md-12">
                            {% render_comment_list for test_list_instance %}
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12 padding-top-20">
                            {% get_comment_form for test_list_instance as form %}

                            <form id="comment-form" method="post">
                                {% csrf_token %}
                                <input id="id_name" maxlength="50" name="name" type="hidden" required="" value="{{ user.username }}">
                                {{ form.object_pk }}
                                {{ form.content_type }}
                                {{ form.security_hash }}
                                {{ form.timestamp }}
                                <textarea cols="40" id="id_comment" maxlength="3000" name="comment" rows="3" required="" placeholder="Add comment" class="margin-bottom-20 form-control autosize"></textarea>

                            </form>
                            <div class="row">
                                <div class="col-md-12">
                                    <button id="post-comment" class="btn btn-flat btn-info btn-sm pull-right" disabled="disabled">Post Comment</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
